from asgiref.sync import sync_to_async
from django.conf import settings
from django.shortcuts import render

# Create your views here.
from .connection import WebSocket
# from devops.models import DeployHistory


async def websocket_view(socket: WebSocket):
    await socket.accept()
    while True:
        message = await socket.receive_text()
        print("receive msg:", message)
        await socket.send_text(message)


async def AppDeployLogsView(socket: WebSocket):
    await socket.accept()
    his_id = socket.query_params.get('id')
    # his = await get_deploy_history(his_id)
    # log_file = settings.DEPLOY_LOGS_PATH + his.log_file
    # if his.log_file:
    #     with open(log_file) as f:
    #         for line in f:
    #             await socket.send_text(line)
    #     await socket.send_text("Success")
    #     return
    await socket.send_text("部署日志为空")
    await socket.close()
    return


# @sync_to_async
# def get_deploy_history(pk):
#     return DeployHistory.objects.get(pk=pk)



